package com.zsj.comminlibrary.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.android.material.snackbar.Snackbar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements IView {

    public P mPresenter;
    protected Context mContext;
    private View mView;

    private ProgressDialog mLoadingDialog;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext = getActivity();
        int viewId = getContentViewResource();
        mView = LayoutInflater.from(getActivity()).inflate(viewId, null);
        initView(mView);
        mPresenter = TUtil.getT(this, 0);
        mLoadingDialog = new ProgressDialog(getActivity());
        mLoadingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        if (mPresenter != null) {
            mPresenter.onSetView(this);
            mPresenter.mContext = mContext;
            mPresenter.onCreate();
        }
        initData();
        return mView;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (mPresenter != null) {
            mPresenter.onStart();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mPresenter != null) {
            mPresenter.onPause();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mPresenter != null) {
            mPresenter.onResume();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mPresenter != null) {
            mPresenter.onStop();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.onDestroy();
        }
    }

    public void showLoading(String msg) {
        mLoadingDialog.setMessage(msg);
        mLoadingDialog.cancel();
        mLoadingDialog.setCanceledOnTouchOutside(false);
        if (mLoadingDialog.isShowing())
            return;
        mLoadingDialog.show();
    }

    public void hideLoading() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

    @Override
    public void exit() {
        getActivity().finish();
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showSnackbar(String msg) {
        View view = getRootView();
        if (view == null) {
            view = ((ViewGroup) getActivity().findViewById(android.R.id.content)).getChildAt(0);
        }
        Snackbar.make(view, msg, 3000)
                .show();
    }

    public View getRootView() {
        return null;
    }

    protected abstract int getContentViewResource();

    protected abstract void initView(View view);

    protected abstract void initData();

}
